/*
 * Copyright (c) 2009 by ZDO Corporation.
 * Author: A. Onur Cinar
 * 
 * http://code.google.com/p/zdo-struts2
 * http://www.zdo.com
 * 
 * This file is part of ZDO Struts 2 Applications.
 *
 * ZDO-Struts2 is free software: you can redistribute it and/or
 * modify it under the terms of  the GNU General Public License
 * as published by the Free Software Foundation, either version
 * 3 of the License, or any later version.
 *
 * ZDO-Struts2 is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY  or  FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 */

package com.zdo.components;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.components.UIBean;
import org.apache.struts2.views.annotations.StrutsTag;
import org.apache.struts2.views.annotations.StrutsTagAttribute;

import com.opensymphony.xwork2.util.ValueStack;

/**
 * Pagination component. Provides pagination support for templates.
 */
@StrutsTag(name = "pagination", 
		tldTagClass = "com.zdo.views.jsp.PaginationTag",
		description = "Provides pagination support for templates.")
public class PaginationComponent extends UIBean
{
	/** Default number of pages to display. */
	public static final int DEFAULT_DISPLAY_PAGES = 5;
	
	private static final String TEMPLATE = "pagination";
	private static final String PARAMETER_TOTAL_PAGES = "totalPages";
	private static final String PARAMETER_CURRENT_PAGE = "currentPage";
	private static final String PARAMETER_DISPLAY_PAGES = "displayPages";
	private static final String PARAMETER_FIRST_PAGE = "firstPage";
	private static final String PARAMETER_LAST_PAGE = "lastPage";
	
	private int totalPages;
	private int currentPage;
	private int displayPages = DEFAULT_DISPLAY_PAGES;
	
	/**
	 * Constructor.
	 * 
	 * @param stack value stack.
	 * @param req servlet request.
	 * @param res servlet response.
	 */
	public PaginationComponent (ValueStack stack, HttpServletRequest req, 
			HttpServletResponse res)
	{
		super(stack, req, res);
	}

	/**
	 * Sets the total number of pages.
	 * 
	 * @param total total pages.
	 */
	@StrutsTagAttribute(description = "Total pages.", type = "Integer")
	public void setTotalPages (String totalPages)
	{
		this.totalPages = Integer.parseInt(findString(totalPages));
	}
	
	/**
	 * Sets the current page.
	 * 
	 * @param current current page.
	 */
	@StrutsTagAttribute(description = "Current page.", type = "Integer")
	public void setCurrentPage (String currentPage)
	{
		this.currentPage = Integer.parseInt(findString(currentPage));
	}
	
	/**
	 * Sets the display pages.
	 * 
	 * @param displayPages display pages.
	 */
	@StrutsTagAttribute(description = "Display pages.", type = "Integer", defaultValue = "5")
	public void setDisplayPages (String displayPages)
	{
		this.displayPages = Integer.parseInt(findString(displayPages));
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	protected String getDefaultTemplate ()
	{
		return TEMPLATE;
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void evaluateExtraParams ()
	{
		super.evaluateExtraParams();
		
		int firstPage;
		int lastPage;
		
		if (currentPage < displayPages)
		{
			firstPage = 1;
			lastPage = Math.min(displayPages * 2, totalPages);
		}
		else if ((totalPages - currentPage) < displayPages)
		{
			firstPage = Math.max(1, totalPages - (2 * displayPages));
			lastPage = totalPages;
		}
		else
		{
			firstPage = currentPage - displayPages;
			lastPage = currentPage + displayPages;
		}
		
		addParameter(PARAMETER_TOTAL_PAGES, totalPages);
		addParameter(PARAMETER_CURRENT_PAGE, currentPage);
		addParameter(PARAMETER_DISPLAY_PAGES, displayPages);
		addParameter(PARAMETER_FIRST_PAGE, firstPage);
		addParameter(PARAMETER_LAST_PAGE, lastPage);
	}
}
